#ifndef _MY_ERROR_H
#define _MY_ERROR_H
#include <setjmp.h>
#include "config.h"

#define	try																\
	if((bufNum >= MAX_REV) || (bufNum < 0)){							\
		fprintf(stderr, "Out of the maximal try-catch recursive number:%d.\n", MAX_REV); \
		exit(0);														\
	}																	\
	errNum = setjmp(resume[bufNum++]);									\
	if(0 == errNum){

#define catch(n)														\
	bufNum--;															\
	}else if((0 != errNum) && (((n) == errNum) || ((n) == ALL_ERROR)))

#define throw(n)								\
	longjmp((resume[--bufNum]),(n))

#define toString(n) errString[n]

typedef enum{
	ERROR_NONE,
	ERROR_TEST, 
	MEMORY_OFFER, 
	DIVSION_BY_ZERO, 
	OUT_OF_RUN,
	/* 其它的错误号 */ 
	ALL_ERROR
}ERROR_E;

const char* errString[] = {
	"No error", 
	"Error test", 
	"Not enough memory", 
	"Divsion by zero", 
	"Out of run"
	/* 错误号所对应的错误信息 */
};

#define MAX_REV 32

int errNum = 0; 
int bufNum = 0;
jmp_buf resume[MAX_REV];
#endif /*_MY_ERROR_H*/

/*main.c*/ 
#include <stdio.h> 
#include <stdlib.h> 
//#include "error.h"

extern int errNum;

void err()
{
	throw(DIVSION_BY_ZERO);
}

int main(void)
{
	int n = 0;
	printf("Input N:");
	scanf("%d",&n);

	try{
		int j = 0; 
		try{
			try{
				try{
					try{
						try{
							err();
							try{
								try{
									try{
										try{
										}catch(ALL_ERROR){
											printf("%d:%s\n", errNum, toString(errNum));
										}
									}catch(ALL_ERROR){
										printf("%d:%s\n", errNum, toString(errNum));
									}
								}catch(ALL_ERROR){
									printf("%d:%s\n", errNum, toString(errNum));
								}
							}catch(ALL_ERROR){
								printf("%d:%s\n", errNum, toString(errNum));
							}
						}catch(ALL_ERROR){
							printf("%d:%s\n", errNum, toString(errNum));
						}
					}catch(ALL_ERROR){
						printf("%d:%s\n", errNum, toString(errNum));
					}
				}catch(ALL_ERROR){
					printf("%d:%s\n", errNum, toString(errNum));
				}
			}catch(ALL_ERROR){
				printf("%d:%s\n", errNum, toString(errNum));
			}
			while(1){
				j++; 
				if(j > 100000){
					throw(OUT_OF_RUN);
				}
			}
		}catch(ALL_ERROR){
			printf("%d:%s\n", errNum, toString(errNum));
		}
		
		if(0 >= n){
			throw(ERROR_TEST);
		}else if(n > 0 ){
			printf("N is:%d\n",n);
		}
	}catch(ALL_ERROR){
		printf("%d:%s\n", errNum, toString(errNum));
	}
	return 0;
} 
